package org.onejava.core.controller;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.onejava.core.bean.Allconfig;
import org.onejava.core.bean.MContext;
import org.onejava.core.exception.MMException;
import org.onejava.core.manage.AopManage;
import org.onejava.core.manage.InterceptorManage;
import org.onejava.core.manage.KActionManage;
import org.onejava.core.manage.PluginManage;
import org.onejava.core.manage.ServiceManage;
import org.onejava.core.manage.ViewManage;
import org.onejava.core.manage.inter.ActionManage;
import org.onejava.core.tools.ScanTool;



/**
 * 主控制器
 * @author frostdb
 */
public class MainController extends HttpServlet {
	private static int flag=0;
	private static Log logger = LogFactory.getLog(MainController.class);
	public void doGet (HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		this.doPost(request,response);
	}
	
	public void doPost (HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		if(flag!=1){
			return;
		}
		long startTime=System.currentTimeMillis();
		//创建MContext
		request.setCharacterEncoding(Allconfig.Charact);
		 MContext context = new MContext(request,response);		 
		//获取ActionController
		ActionController ac =  ActionController.getInstance();
		
		ac.actionDo(context);
		long endTime=System.currentTimeMillis(); //获取结束时间
		logger.info(context.getClassPath()+"执行用时"+(endTime-startTime)+"ms");
	}
	
	public void init(){
		if(flag!=0){
			return;
		}
		flag=2;
		long startTime=System.currentTimeMillis();  
		try {
			initConfig();
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			throw new MMException(e2);
		}
		ScanTool st = new ScanTool();
		Allconfig.truePath=st.getTruePath();
		//加载插件
		PluginManage pm = PluginManage.getManage();
		try {
			pm.intiMap("");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			logger.error("加载插件错误", e1);
			throw new MMException(e1);
		}
		AopManage aopm = AopManage.getMamage();
		try {
			aopm.intiMap(Allconfig.AOP_ROOT);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("加载AOP错误", e);
			throw new MMException(e);
		}
		ServiceManage sm = ServiceManage.getMamage();
		try {
			sm.intiMap(Allconfig.SERVICE_ROOT);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("加载服务错误", e);
			throw new MMException(e);
		}
		//加载视图处理器
		ViewManage vm = ViewManage.getMamage();
		try {
			vm.intiMap(Allconfig.viewRoot);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			logger.error("加载视图错误", e1);
			throw new MMException(e1);
		}
		try {
			//加载拦截器
			InterceptorManage im =InterceptorManage.getManage();
			im.intiMap(Allconfig.InterceptRoot);
			//加载Action
			ActionManage am=null;
			
			
				 am = KActionManage.getActionManage();
			

			am.initActionMap(Allconfig.ACTION_ROOT);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("加载错误", e);
			throw new MMException(e);
		}
		long endTime=System.currentTimeMillis(); //获取结束时间
		flag=1;
		logger.info("加载用时"+(endTime-startTime)+"ms");
		}
	private void initConfig() throws Exception{
		Properties p = new Properties();
		//InputStream is = ClassLoader.getSystemResourceAsStream("/mmvc.properties");
		InputStream is = this.getClass().getResourceAsStream("/mmvc.properties");
		if(null==is){
			return;
		}
		p.load(is);
		String charact = p.getProperty("Charact");
		if(null!=charact){
			Allconfig.Charact=charact;
		}
		String classRoot = p.getProperty("ACTION_ROOT");
		if(null!=classRoot){
			Allconfig.ACTION_ROOT=classRoot;		
				}
		String serviceRoot = p.getProperty("SERVICE_ROOT");
		if(null!=serviceRoot){
			Allconfig.SERVICE_ROOT=serviceRoot;		
				}
		String InterceptRoot = p.getProperty("InterceptRoot");
		if(null!=InterceptRoot){
			Allconfig.InterceptRoot=InterceptRoot;
		}
		String viewRoot = p.getProperty("viewRoot");
		if (null != viewRoot) {
			Allconfig.viewRoot = viewRoot;
		}
		String defaultMethod =p.getProperty("defaultMethod");
		if(null!=defaultMethod){
			Allconfig.defaultMethod=defaultMethod;
		}
		String errorPage =p.getProperty("errorPage");
		if(null!=errorPage){
			Allconfig.errorPage=errorPage;
		}
		String noFindPage =p.getProperty("noFindPage");
		if(null!=noFindPage){
			Allconfig.noFindPage=noFindPage;
		}
		String pageRoot = p.getProperty("pageRoot");
		if(null!=pageRoot){
			Allconfig.pageRoot=pageRoot;
		}
		String springXml = p.getProperty("springXml");
		if(null!=springXml){
			Allconfig.springXml = springXml;
		}
		String iocPluginPath = p.getProperty("iocPluginPath");
		if(null!=iocPluginPath){
			Allconfig.iocPluginPath=iocPluginPath;
		}

		

	}
	public void vaildate() {System.out.println("验证元素");}
}
